//
//  HomepageViewController.swift
//  倔强的大轮胎
//
//  Created by Eiji on 17/3/16.
//  Copyright © 2017年 张迎港. All rights reserved.
//

import UIKit


var CycScorllV = SDCycleScrollView()
var TitleArr = Array<String>()
var detailText = Array<String>()
let myView = HomeTitleView()
var Maintable = UITableView()


class HomepageViewController: UIViewController,UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource {

    override func viewDidLoad() {
        super.viewDidLoad()

        title = "首页"
        view.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        //视图
        createView()
        //滚动视图
        creatScrollV()
        //表格
        createTable()
        //网格
        createColl()
        
        // Do any additional setup after loading the view.
    }
    //MARK: 创建视图
    func createView() {
        //image
        let imageV = UIImageView(frame: CGRect(x: 30, y: 240, width: UIScreen.main.bounds.width-60, height: 38))
        //imageV.image = #imageLiteral(resourceName: "products")
        myView.addSubview(imageV)
        
       
        
        view.addSubview(myView)
        
        
    }
    
    //MARK:  创建网格
    func createColl() {
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = CGSize(width: (UIScreen.main.bounds.size.width-180)/2, height: 135)
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 0
        
        let collectV = UICollectionView(frame: CGRect(x: 160, y: 285, width: UIScreen.main.bounds.size.width-160, height: 135), collectionViewLayout: layout)
        collectV.delegate = self
        collectV.dataSource = self
        collectV.register(UINib.init(nibName: "HomeCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "HomeCollectionViewCell")
        collectV.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        
       myView.addSubview(collectV)
        
        
    }
    //MARK:  创建滚动视图
    func creatScrollV() {
        
        let imageArr = ["banner","HEADER"]
        //初始化滚动视图
        CycScorllV = SDCycleScrollView(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 220))
//        
//        CycScorllV.backgroundColor = #colorLiteral(red: 0.2392156869, green: 0.6745098233, blue: 0.9686274529, alpha: 1)
//        CycScorllV.localizationImageNamesGroup = (imageArr)
//        edgesForExtendedLayout = UIRectEdge(rawValue: 3)
        myView.addSubview(CycScorllV)
        
    }
    
    //MARK: 创建表格
    func createTable() {
        
        
        
     
        TitleArr = ["真空轮胎","中长途轮胎","矿山轮胎"]
        detailText = ["Tubeless tyre","Medium long distance tire","Mine tire"]
        
        
        let table = UITableView(frame: CGRect(x: 0, y: 285, width: 150, height: 45 * TitleArr.count))
        table.tag = 0;
        table.delegate = self
        table.dataSource = self
        myView.addSubview(table)
        
        Maintable = UITableView(frame: UIScreen.main.bounds)
        Maintable.delegate = self
        Maintable.dataSource = self
        Maintable.tag = 1;
        Maintable.register(UINib.init(nibName: "TableViewCellStyle1", bundle: nil), forCellReuseIdentifier: "TableViewCellStyle1")
        Maintable.register(UINib.init(nibName: "TableViewCellStyle2", bundle: nil), forCellReuseIdentifier: "TableViewCellStyle2")
        Maintable.tableHeaderView = myView
        view.addSubview(Maintable)
        
        
        
    }
     //MARK: - UITableViewSource
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if tableView.tag == 0 {
            return 3
        }
        return 9
        
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if tableView.tag == 1 {
            
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if tableView.tag == 0 {
            return 45
        }
        
        return 330
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        switch tableView.tag {
        case 0:
            let str = "ZYG"
            var cell = tableView.dequeueReusableCell(withIdentifier: str)
            if cell == nil {
                cell = UITableViewCell.init(style: .subtitle, reuseIdentifier: str)
            }
            //        cell?.imageView?.image = ImageArr[indexPath.row]as?UIImage
            cell?.textLabel?.text = TitleArr[indexPath.row]
            cell?.textLabel?.font = UIFont.boldSystemFont(ofSize: 11)
            cell?.textLabel?.textAlignment = .center
            cell?.detailTextLabel?.text = detailText[indexPath.row]
            cell?.detailTextLabel?.textAlignment = .center
            cell?.detailTextLabel?.font = UIFont.boldSystemFont(ofSize: 8)
            
            
            
            return cell!

        default:
            
            if indexPath.row % 2 == 0 {
                let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellStyle1", for: indexPath)
                cell.selectionStyle = .none
                return cell
            }
            
            let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellStyle2", for: indexPath)
            
            cell.selectionStyle = .none
            return cell
        }
        
        
        
    }
    //MARK: - UICollectionViewSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 2
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionViewCell", for: indexPath)
        return cell
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
